﻿using System;
using System.Collections.Generic;
using System.Data.Services.Client;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfClient.NorthwindServiceReference;

namespace WpfClient
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		private readonly NorthwindEntities _context = new NorthwindEntities(new Uri(@"http://localhost:3632/NorthwindService.svc/"));
		
		public MainWindow()
		{
			InitializeComponent();
		}

		//private void GetCustomersClick(object sender, RoutedEventArgs e)
		//{
		//    var source = new DataServiceCollection<Customer>(_context.Customers);
		//    dg.ItemsSource = source;
		//}

		//private void SaveClick(object sender, RoutedEventArgs e)
		//{
		//    _context.SaveChanges();
		//    MessageBox.Show("Changes saved");
		//}

		//private void GetCustomerWebClient(object sender, RoutedEventArgs e)
		//{
		//    var uri = _context.BaseUri + "Customers('ALFKI')";

		//    using (var wc = new WebClient())
		//    {
		//        var result = wc.DownloadString(uri);
		//        MessageBox.Show(result);
		//    }

		//    using (var wc = new WebClient())
		//    {
		//        wc.Headers["Accept"] = "application/json";
		//        var result = wc.DownloadString(uri);
		//        MessageBox.Show(result);
		//    }
		//}

		private void Window_Loaded(object sender, RoutedEventArgs e)
		{
			Application.Current.Properties["context"] = _context;
			var source = new DataServiceCollection<Customer>(_context.Customers);
			cmbCustomers.ItemsSource = source;
			cmbCustomers.DisplayMemberPath = "CompanyName";
		}

		private void mnuNewOrder_Click(object sender, RoutedEventArgs e)
		{
			var customer = (Customer) cmbCustomers.SelectedValue;
			var orderWindow = new OrderWindow() {CustomerId = customer.CustomerID};
			orderWindow.ShowDialog();
		}

		private void mnuSave_Click(object sender, RoutedEventArgs e)
		{
			_context.SaveChanges();
			MessageBox.Show("Changes have been saved");
			cmbCustomers_SelectionChanged(null, null);
		}

		private void mnuExit_Click(object sender, RoutedEventArgs e)
		{
			Application.Current.Shutdown();
		}

		private void cmbCustomers_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			var customer = (Customer) cmbCustomers.SelectedValue;
			if(customer == null)
				return;
			lstOrders.ItemsSource = from o in _context.Orders
			                        where o.CustomerID == customer.CustomerID
			                        select new
			                               	{
												o.OrderID,
												o.OrderDate,
												o.RequiredDate
			                               	};
		}
	}
}
